import sys

sys.path.append('../')

from config import *


@app.route('/admin/fill_contact_info.do', methods = ['GET'])
def fill_contact_info():
    if not 'username' in session:
        return redirect(url_for('login'))

    id = request.args.get('id', '').strip()
    info = request.args.get('info', '').strip()

    if not id:
        resp = {
            'success': False,
            'msg': 'invalid resume id'
        }

        response = make_response(jsonify(resp))
        return response

    qushu_contact_coll.update_one({'id': id}, {'$set': {'contact_information': info}})

    resp = {
        'success': True,
        'msg': '成功输入简历简历方式!'
    }

    response = make_response(jsonify(resp))
    return response


@app.route('/admin/account_manager.do', methods = ['GET'])
def account_manager():
    action = request.args.get('action', '').strip()

    if action == 'add':
        username = request.args.get('username', '').strip()
        password = request.args.get('password', '').strip()

        if not username or not password:
            resp = {
                'success': False,
                'msg': '用户名和初始密码不能为空'
            }

            response = make_response(jsonify(resp))
            return response

        if qushu_cv_match_user_coll.find_one({'username': username}):
            resp = {
                'success': False,
                'msg': username + '用户名已存在!'
            }

            response = make_response(jsonify(resp))
            return response

        item = {
            'username': username,
            'password': password,
            'intention_list': [],
            'contact_list': [],
            'role': 'normal',
            'uid': uid(),
            'ctime': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        }

        qushu_cv_match_user_coll.insert_one(item)

        resp = {
            'success': True,
            'msg': '成功创建新用户',
            'data': {
                'username': item['username'],
                'role': item['role'],
                'uid': item['uid'],
                'ctime': item['ctime']
            }
        }

        response = make_response(jsonify(resp))
        return response
